from django.contrib.auth.models import Group
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.signals import post_save

from account.models import User
from utils.mixins.common_mixins.models import BaseModel


# Create your models here.
class Component(BaseModel):
    name = models.CharField(max_length=32, verbose_name='组件名称')
    code = models.CharField(max_length=32, verbose_name='组件编码', null=False, blank=False, unique=True)
    type = models.CharField(max_length=32, verbose_name='组件类型', null=True, blank=True)
    description = models.CharField(max_length=255, verbose_name='组件描述', null=True, blank=True)
    generic_permissions = GenericRelation('GenericPermissions', related_query_name='component')

    class Meta:
        db_table = 'permissions_component'
        verbose_name = '组件表'
        verbose_name_plural = verbose_name


class Route(BaseModel):
    name = models.CharField(max_length=32, verbose_name='路由名称')
    code = models.CharField(max_length=32, verbose_name='路由编码', null=False, blank=False, unique=True)
    path = models.CharField(max_length=255, verbose_name='路由路径', null=False, blank=False, unique=True)
    description = models.CharField(max_length=255, verbose_name='路由描述', null=True, blank=True)
    generic_permissions = GenericRelation('GenericPermissions', related_query_name='route')

    class Meta:
        db_table = 'permissions_route'
        verbose_name = '路由表'
        verbose_name_plural = verbose_name


class Api(BaseModel):
    name = models.CharField(max_length=32, verbose_name='接口名称')
    code = models.CharField(max_length=32, verbose_name='接口编码', null=False, blank=False, unique=True)
    path = models.CharField(max_length=255, verbose_name='接口路径', null=False, blank=False, unique=True)
    method = models.CharField(max_length=32, verbose_name='请求方法', null=False, blank=False)
    description = models.CharField(max_length=255, verbose_name='接口描述', null=True, blank=True)
    generic_permissions = GenericRelation('GenericPermissions', related_query_name='api')

    class Meta:
        db_table = 'permissions_api'
        verbose_name = '接口表'
        verbose_name_plural = verbose_name


class FileTag(BaseModel):
    name = models.CharField(max_length=32, verbose_name='文件标签名称')
    code = models.CharField(max_length=32, verbose_name='文件标签编码', null=False, blank=False, unique=True)
    description = models.CharField(max_length=255, verbose_name='文件标签描述', null=True, blank=True)
    generic_permissions = GenericRelation('GenericPermissions', related_query_name='file_tag')

    class Meta:
        db_table = 'permissions_file_tag'
        verbose_name = '文件标签表'
        verbose_name_plural = verbose_name


class PermissionGroup(models.Model):
    name = models.CharField(max_length=32, verbose_name='权限组名称')
    user = models.ManyToManyField(User, db_constraint=False)
    gp = models.ManyToManyField('GenericPermissions', db_constraint=False, blank=True)

    class Meta:
        db_table = 'v_auth_group'
        verbose_name = ' 组权限设置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class PermissionUser(models.Model):
    password = models.CharField(max_length=128, verbose_name='密码')
    last_login = models.DateTimeField(verbose_name='最后登录时间', null=True, blank=True)
    is_superuser = models.BooleanField(verbose_name='是否是超级用户', default=False)
    username = models.CharField(max_length=150, verbose_name='用户名', unique=True)
    first_name = models.CharField(max_length=30, verbose_name='名字', null=True, blank=True)
    last_name = models.CharField(max_length=150, verbose_name='姓氏', null=True, blank=True)
    email = models.EmailField(verbose_name='邮箱', null=True, blank=True)
    is_staff = models.BooleanField(verbose_name='是否是员工', default=False)
    is_active = models.BooleanField(verbose_name='是否是活跃用户', default=True)
    date_joined = models.DateTimeField(verbose_name='加入时间', auto_now_add=True)
    gp = models.ManyToManyField('GenericPermissions', db_constraint=False, blank=True)

    class Meta:
        db_table = 'v_auth_user'
        verbose_name = ' 用户权限设置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class GenericPermissions(BaseModel):
    name = models.CharField(max_length=32, verbose_name='权限名称')
    code = models.CharField(max_length=32, verbose_name='权限编码', null=False, blank=False)
    status = models.IntegerField(verbose_name='权限状态', default=1)
    description = models.CharField(max_length=255, verbose_name='权限描述', null=True, blank=True)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, db_constraint=False)
    object_id = models.PositiveIntegerField(null=True)
    content_object = GenericForeignKey('content_type', 'object_id')

    class Meta:
        db_table = 'permissions_content_type'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.name} | {self.description}'

